#include "kdclib.h"

#include <QDir>
#include <QFile>
#include <QFileInfo>
#include <QTextStream>

#include <KApplication>
#include <KUrl>
#include <KLocale>
#include <kmountpoint.h>
#include <kio/netaccess.h>

bool isNull( void *ptr )
{
	if ( ptr )
		return false;
	else
		return true;
}

void deletePtr( void *aPtr )
{
	//qDebug( "%d    %d", !isNull( aPtr ), aPtr );
	if ( !isNull( aPtr ) )
		delete aPtr;
	aPtr = NULL;
}

QString toString( KDC::VideoCodec codec )
{
	if ( codec == KDC::MPEG1 )
		return QString( "MPEG-1" );
	else
		if ( codec == KDC::MPEG2 )
			return QString( "MPEG-2" );
	else
		return i18n( "Unknow" );
}

QString toString( KDC::AudioCodec codec )
{
	if ( codec == KDC::AC3 )
		return QString( "AC3" );
	else
		if ( codec == KDC::MP2 )
			return QString( "MP2" );
	else
		if ( codec == KDC::MP3 )
			return QString( "MP3" );
	else
		return i18n( "Unknow" );
}

QString toString( KDC::AspectRatio ar )
{
	if ( ar == KDC::LetterBox )
		return QString( "4:3" );
	else
		return QString( "16:9" );
}

QString toString( KDC::VideoFormat format )
{
	if ( format == KDC::PAL )
		return QString( "PAL" );
	else
		return QString( "NTSC" );
}

QString toString( KDC::Demuxer demuxer )
{
	if ( demuxer == KDC::MPEGPS )
		return QString( "MPEG-PS" );
	else
		return i18n( "Unknow" );
}

QString toString( KDC::StreamMode mode )
{
	if ( mode == KDC::Copy )
		return i18n( "Copy" );
	else
		return i18n( "Resample" );
}

QString toString( KDC::DiscType type )
{
	if ( type == KDC::DVD )
		return i18n( "DVD" );
	if ( type == KDC::SVCD )
		return i18n( "SVCD" );
	if ( type == KDC::VCD )
		return i18n( "VCD" );
	return QString();
}

QString bitrateAsString( int bitrate )
{
	return QString( "%1 kbps" ).arg( bitrate );
}

KDC::AspectRatio aspectRatio(	KDC::DiscType type, double ar, int width, int height, KDC::VideoCodec codec )
{
	if (	isInRange( ar, 1.76, 1.8 ) && type == KDC::DVD &&
				( ( codec == KDC::MPEG2 && width == 720 && ( height == 576 || height == 480 ) ) ||
					( codec == KDC::MPEG1 && width == 352 && ( height == 288 || height == 240 ) ) ) )
		return KDC::WideScreen;
	else
		return KDC::LetterBox;
}

bool isInRange( int value, int min, int max )
{
	if ( ( value <= max ) && ( value >= min ) )
		return TRUE;
	else
		return FALSE;
}

bool isInRange( double value, double min, double max )
{
	if ( ( value <= max ) && ( value >= min ) )
		return TRUE;
	else
		return FALSE;
}


int getCorrectHeight( int width, int oldHeight, KDC::VideoFormat format )
{
	if ( format == KDC::PAL )
	{
		if ( width == 352 || ( oldHeight - 240 ) == 0 )
			return 288;
		return 576;
	}
	else
	{
		if ( width == 352 || ( oldHeight - 288 ) == 0 )
			return 240;
		return 480;
	}
}

bool isPALDVDResolution( int width, int height )
{
	if ( height == 576 )
	{
		if ( width == 720 || width == 704 || width == 352 )
			return TRUE;
		else
			return FALSE;
	}	
	else
		if ( height == 288 && width == 352 )
			return TRUE;
	else
		return FALSE;
}

bool isNTSCDVDResolution( int width, int height )
{
	if ( height == 480 )
	{
		if ( width == 720 || width == 704 || width == 352 )
			return TRUE;
		else
			return FALSE;
	}	
	else
		if ( height == 240 && width == 352 )
			return TRUE;
	else
		return FALSE;
}

QString fileName( const QString& path )
{
	QFileInfo info( path );
	return info.fileName();
}

QString baseFileName( const QString& path )
{
	QFileInfo info( path );
	return info.completeBaseName();
}

QString filePath( const QString& path )
{
	QFileInfo info( path );
	return info.absolutePath();
}

QString fileNameWithoutExtension( const QString& path )
{
	QFileInfo info( path );
	QString str;
	str.append( info.absolutePath() );
	str.append( "/" );
	str.append( info.completeBaseName() );
	return str;
}

QString readAll( const QString& fileName )
{
	QFile in( fileName );
	in.open( QIODevice::ReadOnly );
	QTextStream st( &in );
	QString out = st.readAll();
	in.close();
	return out;
}

void writeToFile( const QString& fileName, const char *msg, bool append )
{
	QFile file( fileName );
	if ( append )
		file.open( QIODevice::WriteOnly | QIODevice::Append );
	else
		file.open( QIODevice::WriteOnly );
	QTextStream stream( &file );
	stream << QString( msg ) + "\n";
	file.close();
}

QString mountPoint( const QString& path )
{
	KMountPoint::List mountList = KMountPoint::currentMountPoints();
	KMountPoint *ptr = mountList.findByPath( path ).data();

	if ( ptr )
		return ptr->mountPoint();
	else
		return "/";
}

bool createDirectory( const QString& path )
{
	QDir dir( path );
	if ( !dir.exists() )
		return dir.mkpath( path );//res = KIO::NetAccess::mkdir( KUrl( path ), NULL );
	else
		return TRUE;
}

void removeDirectory( const QString& path )
{
	QDir dir( path );
	if ( dir.exists() )
		KIO::NetAccess::del( KUrl( path ), NULL );
}

bool removeFile( const QString& path )
{
	if ( QFile::exists( path ) )
		return QFile::remove( path );
	else
		return FALSE;
}

bool renameFile( const QString& oldName , const QString& newName )
{
	if ( QFile::exists( oldName ) && !QFile::exists( newName ) )
		return QFile::rename( oldName, newName );
	else
		return FALSE;
}


KDCDisc* getDisc( KDCObject *aObject )
{
	if ( isNull( aObject ) )
		return NULL;
	KDCObject *obj;
	if ( aObject->object() == KDC::Disc )
		obj = aObject;
	else
		obj = aObject->parent();
	while ( !isNull( obj ) && obj->object() != KDC::Disc )
		obj = obj->parent();
	return dynamic_cast<KDCDisc*>( obj );
}

KDCVTS* getVTS( KDCObject *aObject )
{
	if ( isNull( aObject ) || aObject->object() == KDC::Disc )
		return NULL;
	KDCObject *obj;
	if ( aObject->object() == KDC::VTS )
		obj = aObject;
	else
		obj = aObject->parent();
	while ( !isNull( obj ) && obj->object() != KDC::VTS )
		obj = obj->parent();
	return dynamic_cast<KDCVTS*>( obj );
}

QString movieNameFromPos( KDCMovie *aMovie, int aPos )
{
	if ( isNull( aMovie ) )
		return QString();
	QString title = aMovie->metaData()->name();
	int totalLength = aMovie->video()->metaData()->length();
	if ( totalLength >= aPos )
		return title;
	for ( int i=0; i<aMovie->subMovies().size(); ++i )
	{
		totalLength += aMovie->subMovies().at( i )->video()->metaData()->length();
		title = aMovie->subMovies().at( i )->metaData()->name();
		if ( totalLength >= aPos )
			break;
	}
	return title;
}

int moviePos( KDCMovie *aMovie, int aPos )
{
	if ( isNull( aMovie ) )
		return 0;
	int totalLength = aMovie->video()->metaData()->length();
	if ( totalLength >= aPos )
		return aPos;
	for ( int i=0; i<aMovie->subMovies().size(); ++i )
	{
		totalLength += aMovie->subMovies().at( i )->video()->metaData()->length();
		if ( totalLength >= aPos )
			return totalLength - aPos;
	}
	return 0;
}

QString applicationDataDir()
{
	QDir dir ( KApplication::applicationDirPath() );
	dir.cdUp();
	dir.cd( "data" );
	return dir.absolutePath();
}